package com.ewanghuiju.app.ui.login.activity;

import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ewanghuiju.app.R;
import com.ewanghuiju.app.app.App;
import com.ewanghuiju.app.app.Constants;
import com.ewanghuiju.app.base.BaseActivity;
import com.ewanghuiju.app.base.contract.login.PhoneLoginContract;
import com.ewanghuiju.app.model.bean.request.LoginRequestBean;
import com.ewanghuiju.app.model.bean.response.LoginResponBean;
import com.ewanghuiju.app.model.bean.response.UserInfoResponBean;
import com.ewanghuiju.app.model.prefs.ImplPreferencesHelper;
import com.ewanghuiju.app.presenter.login.PhoneLoginPresenter;
import com.ewanghuiju.app.ui.main.activity.MainActivity;
import com.ewanghuiju.app.ui.web.MyWebviewActivity;
import com.ewanghuiju.app.util.LoadingDialogUtils;
import com.ewanghuiju.app.util.StartActivityUtil;
import com.ewanghuiju.app.util.StringUtil;
import com.ewanghuiju.app.util.nodoubleclick.AntiShake;
import com.ewanghuiju.app.widget.EditTextWithDel;
import com.gyf.immersionbar.ImmersionBar;
import com.muddzdev.styleabletoast.StyleableToast;

import androidx.appcompat.widget.AppCompatCheckBox;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author sj  qq:751943509
 * @class describe
 * @time 2020/2/14
 */
public class PhoneLoginActivity extends BaseActivity<PhoneLoginPresenter> implements PhoneLoginContract.View {
    @BindView(R.id.et_phone)
    EditTextWithDel mEtphone;
    @BindView(R.id.et_pwd)
    EditTextWithDel mEtpwd;
    @BindView(R.id.iv_eye)
    ImageView mImgeye;
    @BindView(R.id.tv_phone_tip)
    TextView tv_phone_tip;
    @BindView(R.id.tv_pwd_tip)
    TextView tv_pwd_tip;
    @BindView(R.id.login_text)
    TextView loginText;
    @BindView(R.id.tv_login)
    TextView tvLogin;
    @BindView(R.id.center_view)
    LinearLayout centerView;
    @BindView(R.id.layout_xieyi)
    LinearLayout layoutXieyi;
    @BindView(R.id.checkbox)
    AppCompatCheckBox checkbox;

    private boolean isOpenEye = false;
    private Animation shake;

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_phone_login;
    }

    @Override
    protected void initEventAndData() {
        //密码不可见
        mEtpwd.setTransformationMethod(PasswordTransformationMethod.getInstance());

        shake = AnimationUtils.loadAnimation(this, R.anim.shake);
        tv_phone_tip.startAnimation(shake);
//        mEtpwd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); 控制密码显示隐藏
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this)
                .titleBar(mToolbar, true)
                .transparentBar()
                .statusBarDarkFont(true)
                .init();
    }

    @OnClick({R.id.tv_login, R.id.tv_regist, R.id.tv_login_by_code, R.id.tv_forget_pwd, R.id.tv_yhxy, R.id.tv_yszc})
    public void doClick(View view) {
        if (AntiShake.check(view.getId())) {    //判断是否多次点击
            return;
        }
        switch (view.getId()) {
            case R.id.tv_login:
                loginByPhonePwd();
                break;
            case R.id.tv_regist:
                new StartActivityUtil(mContext, PhoneRegistbindActivity.class).putExtra(Constants.LOGIN_TYPE, 1)
                        .startActivity(true);
                break;
            case R.id.tv_login_by_code:
                new StartActivityUtil(mContext, CodeLoginActivity.class).putExtra(Constants.BIND_USERINFO, 1)
                        .startActivity(true);
                break;
            case R.id.tv_forget_pwd:
                new StartActivityUtil(mContext, PhoneRegistbindActivity.class).putExtra(Constants.LOGIN_TYPE, 2)
                        .startActivity(true);
                break;
            case R.id.tv_yhxy:
                //用户协议
                new StartActivityUtil(mContext, MyWebviewActivity.class).putExtra("url", Constants.WEBURL_USERXY)
                        .putExtra(Constants.WEBURL_TITLE, "用户协议").startActivity(true);
                break;
            case R.id.tv_yszc:
                //隐私政策
                new StartActivityUtil(mContext, MyWebviewActivity.class).putExtra("url", Constants.WEBURL_USERXY)
                        .putExtra(Constants.WEBURL_TITLE, "隐私政策").startActivity(true);
                break;
            default:
                break;
        }
    }

    @OnClick({R.id.iv_eye})
    public void showHideEye() {
        if (!isOpenEye) {
            mImgeye.setImageResource(R.mipmap.btn_equip_setting_eye_off);
            isOpenEye = true;
            //密码可见
            mEtpwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            mEtpwd.setSelection(mEtpwd.getText().toString().length());//将光标移至文字末尾
        } else {
            mImgeye.setImageResource(R.mipmap.btn_equip_setting_eye_on);
            isOpenEye = false;
            //密码不可见
            mEtpwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
            mEtpwd.setSelection(mEtpwd.getText().toString().length());//将光标移至文字末尾
        }
    }

    /**
     * 手机号密码登录
     */
    private String phoneNum;

    private void loginByPhonePwd() {
//        mEtphone.setText("15665282836");
//        mEtpwd.setText("123456");
//        checkbox.setChecked(true);
        phoneNum = "";
        if (!checkbox.isChecked()) {
            StyleableToast.makeText(mContext, "请选择用户协议", Toast.LENGTH_SHORT, R.style.mytoast).show();
            return;
        }
        phoneNum = mEtphone.getText().toString();
        if (TextUtils.isEmpty(phoneNum)) {
            StyleableToast.makeText(mContext, "请输入手机号", Toast.LENGTH_SHORT, R.style.mytoast).show();
            return;
        }

        if (!StringUtil.isPhone(phoneNum)) {
            StyleableToast.makeText(mContext, "请输入正确手机号", Toast.LENGTH_SHORT, R.style.mytoast).show();
            return;
        }

        String pwdStr = mEtpwd.getText().toString();
        if (TextUtils.isEmpty(pwdStr)) {
            StyleableToast.makeText(mContext, "请输入密码", Toast.LENGTH_SHORT, R.style.mytoast).show();
            return;
        }
        LoadingDialogUtils.show(mContext);
        LoginRequestBean loginRequestBean = new LoginRequestBean();
        loginRequestBean.setMobile(phoneNum);
        loginRequestBean.setPwd(pwdStr);
        loginRequestBean.setType("1");
        loginRequestBean.setCode("");
        mPresenter.phoneLogin(loginRequestBean);
    }

    @Override
    public void showLogin(LoginResponBean bean) {
        LoadingDialogUtils.dismissDialog_ios();
//        if (bean != null && !TextUtils.isEmpty(bean.getIs_bind_parent()) && "1".equals(bean.getIs_bind_parent())) {
        if (bean != null && !TextUtils.isEmpty(bean.getToken())) {
            ImplPreferencesHelper mImplPreferencesHelper = App.getAppComponent().preferencesHelper();
            mImplPreferencesHelper.setToken(bean.getToken());
            //获取用户信息  到达首页
            LoadingDialogUtils.show(mContext);
            mPresenter.getUserInfo();
        }
//        }
        else {
            //绑定用户信息
            new StartActivityUtil(mContext, AddInvitecodeActivity.class).putExtra(Constants.REGIST_PHONE, phoneNum).startActivity(true);
        }
    }

    @Override
    public void showLoginResponseMsg(int code, String message) {
        LoadingDialogUtils.dismissDialog_ios();
        StyleableToast.makeText(mContext, message, Toast.LENGTH_SHORT, R.style.mytoast).show();
    }

    @Override
    public void showUserInfo(UserInfoResponBean bean) {
        LoadingDialogUtils.dismissDialog_ios();
        StyleableToast.makeText(mContext, "登录成功", Toast.LENGTH_SHORT, R.style.mytoast).show();
        new StartActivityUtil(mContext, MainActivity.class)
                .startActivity(false);
        onBackPressedSupport();
        slideRightIn();
    }

    @Override
    public void showUserInfoEorrMsg(int code, String message) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (shake != null) {
            shake.cancel();
            shake = null;
        }
    }
}
